package com.zx.zxpicture.infrstructure.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则工具类
 */
@Slf4j
public class RegexUtils {


    /**
     * 根据正则表达式匹配字符串中的子串
     * @param input 输入字符串
     * @param regex 正则表达式
     * @return 匹配到的子字符串集合（如果没有匹配项则返回空集合）
     */
    public static String findMatchingSubstrings(String input, String regex, String delimiter) {
        if (input == null || regex == null || input.isEmpty() || regex.isEmpty()) {
            return "";
        }

        StringBuilder result = new StringBuilder();
        try {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(input);

            while (matcher.find()) {
                if (result.length() > 0) {
                    result.append(delimiter); // 使用自定义分隔符
                }
                result.append(matcher.group());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("字符串：{}匹配到的子字符串：{}",input, result);
        return result.toString();
    }

    // 测试用例
    public static void main(String[] args) {
        String test1 = "https://zx-1343343346.cos.ap-chongqing.myqcloud.com/picture/private/1895070808804982786/2025-02-27_DAKJoZvY9o.webp";
        String text = "https://zx-1343343346.cos.ap-chongqing.myqcloud.com/picture/public/1891475289071927298/2025-02-25_6VF9kkj7wF.JwQ3k3f5VZDzUag4U7WoTAHaJ3";
        String regex = "/picture/private/[\\d]+/[\\d]{4}-[\\d]{2}-[\\d]{2}_[[a-zA-Z0-9]]{10}(_thumbnail)*.[a-zA-Z0-9]+"; // 匹配手机号格式

        System.out.println(findMatchingSubstrings(test1, regex,""));
    }
}